package test;

import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author: cuiyutian1
 * @date: 2021/9/3 13:57
 * @description:
 */
public class MapConvert {

    public static String mapToJoin(Map<String, Object> map) {
        StringBuilder builder = new StringBuilder("");
        String[] str = map.keySet().toArray(new String[] {});
        List<String> strings = Arrays.asList(str);
        Collections.sort(strings);
        int length = strings.size();
        int i = 0;
        for (String string : strings) {
            String val = map.get(string).toString();
            if (val.startsWith("{") || val.startsWith("[")) {
                val = URLEncoder.encode(val);
            }
            if ("timestamp".equals(string)) {
                val = URLEncoder.encode(val);
            }
            builder.append(string).append("=").append(val);
            i++;
            if (i < length) {
                builder.append("&");
            }
        }
        ;
        return builder.toString();
    }
    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("map","map");
        map.put("map2","map2");
        map.put("{","{");
        System.out.println(mapToJoin(map));
    }

}
